﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccontrols;

namespace Buccaneer
{
    public partial class ControlsControl : PluginControl
    {
        BitmapButton bbn_go, bbn_close;

        public ControlsControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);

            this.bbn_go = AddControlGo();
            this.bbn_go.Click += new EventHandler(bbn_go_Click);
            this.bbn_close = AddControlClose();
            this.bbn_close.Click += new EventHandler(bbn_close_Click);
            this.Controls.Add(this.bbn_go);
            this.Controls.Add(this.bbn_close);
        }

        public static void setFlatAppearance(ref BitmapButton bitmapButton)
        {
            bitmapButton.TabStop = true;
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            return imgToResize.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
        }

        public static void loadImage(ref BitmapButton bitmapButton, string imageName)
        {
            try
            {
                Options.WriteComment(" -  loading image " + imageName + "...");
                bitmapButton.Image = Options.GetResource(imageName) as Image;
                Options.WriteComment("(i) Succeeded.");
            }
            catch (Exception ex)
            {
                Options.WriteComment("(X) An error occurred:" + ex.Message + "\nsource:" + ex.Source);
                MessageBox.Show("Error in loading the following image: " + imageName + "\n\nException info: " + ex.Message);
                bitmapButton.Image = null;
                return;
            }
            bitmapButton.LoadImages(imageName);
        }

        #region Control

        public static BitmapButton AddControlGo()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(0, 0);
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            loadImage(ref bitmapButton, "ctrlGo");
            bitmapButton.Size = bitmapButton.Image.Size;
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }


        public static BitmapButton AddControlClose()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(51, 0);
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            loadImage(ref bitmapButton, "ctrlClose");
            bitmapButton.Size = bitmapButton.Image.Size;
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }

        #endregion


        #region Eventhandlers

        void bbn_close_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("exit", new string[] { "exit button clicked" });
        }

        void bbn_go_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("navigate", new string[] { "C:\\" });
        }

        #endregion
    }
}
